﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Exist
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Process externalProcess;

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void ButtonLaunch_Click(object sender, RoutedEventArgs e)
        {
            // 检查按钮是否已经被禁用（在这个简单示例中可能不是必需的，但为完整性而添加）
            if (!buttonLaunch.IsEnabled)
            {
                return;
            }

            // 启动外部程序
            string exePath = @"./VisualCodeEditor\Assembly.exe"; // 替换为实际的exe路径
            try
            {
                externalProcess = new Process
                {
                    StartInfo = new ProcessStartInfo(exePath)
                    {
                        UseShellExecute = true, // 对于某些应用程序，可能需要设置为true
                        CreateNoWindow = false  // 根据需要设置，如果不需要新窗口则设置为true
                    }
                };
                buttonLaunch.IsEnabled = false; // 禁用按钮


                // 订阅进程退出事件
                externalProcess.EnableRaisingEvents = true;
                externalProcess.Exited += ExternalProcess_Exited;

                externalProcess.Start();
                //await Task.Run(() => externalProcess.WaitForExit());
            }
            catch (Exception ex)
            {
                MessageBox.Show($"启动外部程序失败: {ex.Message}");
            }
        }

        private void ExternalProcess_Exited(object sender, EventArgs e)
        {
            // 当外部进程退出时，重新启用按钮
            Dispatcher.Invoke(() =>
            {
                buttonLaunch.IsEnabled = true;
            });
        }
    }
}
